clear all
set more off
cap log close

adopath + "H:\Lavecchia_7086\to-transfer-jan-2022\reghdfe_files"
adopath + "H:\Lavecchia_7086\to-transfer-jan-2022\binscatter_files"


do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"

****************************************************************************
* This do-file uses subsamples
*	- capital gains, income, dividends and rrsp related variables
* 
* Regressions
* 
********************************************************************************

************* RUN INDIVIUDAL FIGURES/TABLES ON THEIR OWN ***********************

local figure2=0
local figure4=0
local figure5=0
local figure6=0
local figure7=0
local figure8=1
local figure9=0

********************************************************************************


**************** FIGURE 2: CALCULATE LIFETIME CAPITAL GAINS **************

if (`figure2') {

cap log close
log using "$dir_log\RESTAT_Figure_2.log", replace

 *   weighted true
 foreach sample in weighted true {
 
****************************************************************************
*use "$dir_data\capital_income_final_smallcombined.dta"
clear
use "$dir_data\capital_income_final_8299.dta"
append using "$dir_data\capital_income_final_0016.dta"
****************************************************************************
keep lin__i year wgt2_i clkgxi cpi_to2016
 
* merge with age
sort lin__i year
merge 1:1 lin__i year using  "$dir_data\age.dta"
drop if _merge==2
drop _merge
gen age82=age-(year-1982)

keep if age82>=30 & age82<=35
drop age__i age82

gen total=1
gen pos_clkgxi=(clkgxi>0) /* number of individuals who report positive */
gen pos_clkgxi1k=(clkgxi*cpi_to2016>2338.8) /* number of individuals who report positive >1k */
bysort lin__i (year): gen cum_pos_clkgxi=sum(pos_clkgxi)
gen ever_pos_clkgxi=(cum_pos_clkgxi>=1)
bysort lin__i (year): gen cum_pos_clkgxi1k=sum(pos_clkgxi1k)
gen ever_pos_clkgxi1k=(cum_pos_clkgxi1k>=1)


replace clkgxi=clkgxi*cpi_to2016/1.6378 /* inflation adjust to 1990 */
bysort lin__i (year): gen csum_clkgxi=sum(clkgxi)

_pctile clkgxi, percentiles(75 99)
gen total_75=1 if clkgxi<=r(r1)
gen total_99=1 if clkgxi<=r(r2)
gen clkgxi_75=clkgxi if clkgxi<=r(r1)
gen clkgxi_99=clkgxi if clkgxi<=r(r2)
_pctile csum_clkgxi, percentiles(75 99)
gen csum_total_75=1 if csum_clkgxi<=r(r1)
gen csum_total_99=1 if csum_clkgxi<=r(r2)
gen csum_clkgxi_75=csum_clkgxi if csum_clkgxi<=r(r1)
gen csum_clkgxi_99=csum_clkgxi if csum_clkgxi<=r(r2)

if "`sample'"=="true"{

*  DOMINANCE TEST
foreach var in clkgxi csum_clkgxi  clkgxi_75 clkgxi_99 csum_clkgxi_75 csum_clkgxi_99    {
	bysort year: egen max1_`var'=max(`var')
	gen temp=`var' if `var'!=max1_`var'
	bysort year: egen max2_`var'=max(temp)
	replace temp=.
	replace temp=`var' if `var'!=max1_`var' & `var'!=max2_`var'
	bysort year: egen max3_`var'=max(temp)
	gen sum_`var'=`var' if `var'!=max1_`var' & `var'!=max2_`var' & `var'!=max3_`var'
	drop temp max2_`var' max3_`var'
}

collapse  (sum)  total pos_clkgxi pos_clkgxi1k  ever_pos_clkgxi ever_pos_clkgxi1k total_75 total_99 csum_total_75 csum_total_99 ///
		  (mean) clkgxi cum_pos_clkgxi cum_pos_clkgxi1k csum_clkgxi (p99) clkgxi_p99=clkgxi csum_clkgxi_p99=csum_clkgxi (p75) clkgxi_p75=clkgxi csum_clkgxi_p75=csum_clkgxi  ///
		  (mean) max1_clkgxi max1_csum_clkgxi max1_clkgxi_75 max1_clkgxi_99 max1_csum_clkgxi_75 max1_csum_clkgxi_99 ///
		  (sum) sum_clkgxi sum_csum_clkgxi sum_clkgxi_75 sum_clkgxi_99 sum_csum_clkgxi_75 sum_csum_clkgxi_99 ///
 ,   by(year)
 
 * PASS DOMINANCE TEST
foreach var in clkgxi csum_clkgxi  clkgxi_75 clkgxi_99 csum_clkgxi_75 csum_clkgxi_99  {
gen domtest_`var'=(max1_`var'/sum_`var'>0.8)
gen rvalue_`var'=max1_`var'/sum_`var'
}

} 
 
 
if "`sample'"=="weighted"{

collapse  (sum)  total pos_clkgxi pos_clkgxi1k  ever_pos_clkgxi ever_pos_clkgxi1k total_75 total_99 csum_total_75 csum_total_99 ///
		  (mean) clkgxi cum_pos_clkgxi cum_pos_clkgxi1k csum_clkgxi (p99) clkgxi_p99=clkgxi csum_clkgxi_p99=csum_clkgxi  (p75) clkgxi_p75=clkgxi csum_clkgxi_p75=csum_clkgxi  ///
 [w=wgt2_i] ,   by(year)
 
 
 * ROUND COUNTS &  inflate by 5 
foreach var in total pos_clkgxi pos_clkgxi1k  ever_pos_clkgxi ever_pos_clkgxi1k total_75 total_99 csum_total_75 csum_total_99 {
gen r`var'=round(`var'*5,5)
}


* ROUND AMOUNTS 
foreach var in  clkgxi  csum_clkgxi  clkgxi_p75 csum_clkgxi_p99  csum_clkgxi_p75  {
	gen r`var'=.
	replace r`var'=round(`var',10) if `var'<=1000
	replace r`var'=round(`var',100) if `var'>1000
}
}

save "$dir_results\RESTAT_Figure_2_lifetime_gains_graphs_`sample'.dta", replace
export excel using "$dir_results\RESTAT_Figure_2_lifetime_gains_graphs_`sample'.xlsx", firstrow(variables) replace	

}

}


***************** FIGURE 4: CG Shares by CG_8593 GROUP *************************

if (`figure4') {
	
cap log close
log using "$dir_log\RESTAT_Figure_4.log", replace

clear	
use "$dir_data\data_sample_IV_diffndiff.dta" 
gen flag = (clkgxi8593 == .)
drop if flag == 1
drop flag

keep lin__i clkgxi year wgt2_i clkgxi8593 cg_8593
gen group_5h = (clkgxi8593 >= 25000 & clkgxi8593 < 50000)

gen group = 1 if cg_8593 == 1
replace group = 2 if cg_8593 == 2
replace group = 3 if cg_8593 == 3
replace group = 4 if cg_8593 == 4
replace group = 5 if cg_8593 == 5 & group_5h == 1
replace group = 6 if cg_8593 == 5 & group_5h == 0
replace group = 7 if cg_8593 == 6

gen weight2 = 5*wgt2_i

* Method 1: No weights
by year, sort : egen float total_clkgxi_year_nowgt = total(clkgxi)
by group year, sort : egen float total_clkgxi_group_year_nowgt = total(clkgxi)
by group year, sort : egen float count_group_year_nowgt = count(clkgxi)

tab year group
by year group, sort : summarize total_clkgxi_year_nowgt total_clkgxi_group_year_nowgt count_group_year_nowgt
help round
gen total_clkgxi_gp_yr_round_nowgt = round(total_clkgxi_group_year_nowgt,100)
gen total_clkgxi_yr_round_nowgt = round(total_clkgxi_year_nowgt,100)
gen share_clkgxi_gp_yr_nowgt = total_clkgxi_gp_yr_round_nowgt / total_clkgxi_yr_round_nowgt


* Method 2: Use Disturbance Weight
gen clkgxi_wgt = clkgxi*wgt2_i

by year, sort : egen float total_clkgxi_year = total(clkgxi_wgt)
by group year, sort : egen float total_clkgxi_group_year = total(clkgxi_wgt)
by group year, sort : egen float count_group_year = count(clkgxi_wgt)

tab year group
by year group, sort : summarize total_clkgxi_year total_clkgxi_group_year count_group_year
help round
gen total_clkgxi_group_year_round = round(total_clkgxi_group_year_nowgt,100)
gen total_clkgxi_year_round = round(total_clkgxi_year,100)
gen share_clkgxi_group_year = total_clkgxi_group_year_round / total_clkgxi_year_round


collapse (mean) total_clkgxi_group_year_round total_clkgxi_year_round share_clkgxi_group_year total_clkgxi_gp_yr_round_nowgt total_clkgxi_yr_round_nowgt share_clkgxi_gp_yr_nowgt total_clkgxi_year total_clkgxi_group_year count_group_year total_clkgxi_year_nowgt total_clkgxi_group_year_nowgt count_group_year_nowgt, by(group year)

save "$dir_results\RESTAT_Figure_4.dta", replace
export excel using "$dir_results\RESTAT_Figure_4.xlsx", firstrow(variables) replace	
				
	
}


***************** FIGURE 5: RAW MEANS (EXTENSIVE & INTENSIVE) ******************

if (`figure5') {

cap log close
log using "$dir_log\RESTAT_Figure_5.log", replace


clear
use "$dir_data/data_sample_DD_unconditional.dta"

merge lin__i year using "$dir_data/province_temp.dta"
replace province = province2 if province == . & year >= 2000
drop province2
keep if _merge == 3
drop _merge

gen flag_missing = (clkgxi8593 == .)
drop if flag_missing == 1
drop flag_missing

drop log_clkgxi
gen log_clkgxi = log(clkgxi)
drop ihs_clkgxi
gen ihs_clkgxi = log(clkgxi + sqrt(1 + clkgxi)^2)


gen married = (fcmp_i == 1 | fcmp_i == 11 | fcmp_i == 2 | fcmp_i == 12 | fcmp_i == 5 | fcmp_i == 15)
keep year cg_8593 wgt2_i age married tnkidi tirc_i txi__i log_clkgxi clkgxi ihs_clkgxi cpi_to2016 

* (1) number of individ with positive gains
gen total=1
ge clkgxi_dummy = (clkgxi > 0)

* (2) summary stats for those with positive gains
gen pos_age=age if clkgxi>0 
gen pos_married=married if clkgxi>0 
gen pos_tnkidi=tnkidi if clkgxi>0 
gen pos_tirc_i=tirc_i if clkgxi>0
gen pos_txi__i=txi__i if clkgxi>0
gen pos_clkgxi = clkgxi if clkgxi>0
gen pos_log_clkgxi = log_clkgxi if clkgxi>0
gen pos_ihs_clkgxi = ihs_clkgxi if clkgxi>0


* (3) Generate weighted/perturb versions of the variables of interest
foreach var in age married tnkidi tirc_i txi__i clkgxi log_clkgxi ihs_clkgxi clkgxi_dummy pos_age pos_married pos_tnkidi pos_tirc_i pos_txi__i pos_clkgxi pos_log_clkgxi pos_ihs_clkgxi {
	gen `var'_wgt = `var'*wgt2_i
}


*  DOMINANCE TEST
foreach var in  tirc_i txi__i clkgxi log_clkgxi ihs_clkgxi clkgxi_dummy pos_tirc_i pos_txi__i pos_clkgxi pos_log_clkgxi pos_ihs_clkgxi  {
	bysort cg_8593 year: egen max1_`var'=max(`var')
	gen temp=`var' if `var'!=max1_`var'
	bysort cg_8593 year: egen max2_`var'=max(temp)
	replace temp=.
	replace temp=`var' if `var'!=max1_`var' & `var'!=max2_`var'
	bysort cg_8593 year: egen max3_`var'=max(temp)
	gen sum_`var'=`var' if `var'!=max1_`var' & `var'!=max2_`var' & `var'!=max3_`var'
	drop temp max2_`var' max3_`var'
}

gen clkgxi_count = clkgxi_dummy


collapse (sum) total clkgxi_count (mean) age married tnkidi tirc_i txi__i clkgxi log_clkgxi ihs_clkgxi clkgxi_dummy pos_age pos_married pos_tnkidi pos_tirc_i pos_txi__i pos_clkgxi pos_log_clkgxi pos_ihs_clkgxi (semean) se_age=age se_married=married se_tnkidi=tnkidi se_tirc_i=tirc_i se_txi__i=txi__i se_clkgxi=clkgxi se_log_clkgxi=log_clkgxi se_ihs_clkgxi=ihs_clkgxi se_clkgxi_dummy=clkgxi_dummy se_pos_age=pos_age se_pos_married=pos_married se_pos_tnkidi=pos_tnkidi se_pos_tirc_i=pos_tirc_i se_pos_txi__i=pos_txi__i se_pos_clkgxi=pos_clkgxi se_pos_log_clkgxi=pos_log_clkgxi se_pos_ihs_clkgxi=pos_ihs_clkgxi (mean) age_wgt married_wgt tnkidi_wgt tirc_i_wgt txi__i_wgt clkgxi_wgt log_clkgxi_wgt ihs_clkgxi_wgt clkgxi_dummy_wgt pos_age_wgt pos_married_wgt pos_tnkidi_wgt pos_tirc_i_wgt pos_txi__i_wgt pos_clkgxi_wgt pos_log_clkgxi_wgt pos_ihs_clkgxi_wgt (semean) se_age_wgt=age_wgt se_married_wgt=married_wgt se_tnkidi_wgt=tnkidi_wgt se_tirc_i_wgt=tirc_i_wgt se_txi__i_wgt=txi__i_wgt se_clkgxi_wgt=clkgxi_wgt se_log_clkgxi_wgt=log_clkgxi_wgt se_ihs_clkgxi_wgt=ihs_clkgxi_wgt se_clkgxi_dummy_wgt=clkgxi_dummy_wgt se_pos_age_wgt=pos_age_wgt se_pos_married_wgt=pos_married_wgt se_pos_tnkidi_wgt=pos_tnkidi_wgt se_pos_tirc_i_wgt=pos_tirc_i_wgt se_pos_txi__i_wgt=pos_txi__i_wgt se_pos_clkgxi_wgt=pos_clkgxi_wgt se_pos_log_clkgxi_wgt=pos_log_clkgxi_wgt se_pos_ihs_clkgxi_wgt=pos_ihs_clkgxi_wgt  (mean) max1_tirc_i max1_txi__i max1_clkgxi max1_log_clkgxi max1_ihs_clkgxi max1_pos_tirc_i  max1_pos_txi__i max1_pos_clkgxi max1_pos_log_clkgxi max1_pos_ihs_clkgxi (sum) sum_tirc_i=tirc_i sum_txi__i=txi__i sum_clkgxi=clkgxi sum_log_clkgxi=log_clkgxi sum_ihs_clkgxi=ihs_clkgxi sum_clkgxi_dummy=clkgxi_dummy sum_pos_tirc_i=pos_tirc_i sum_pos_txi__i=pos_txi__i sum_pos_clkgxi=pos_clkgxi sum_pos_log_clkgxi=pos_log_clkgxi sum_pos_ihs_clkgxi=pos_ihs_clkgxi, by(cg_8593 year)
		


* PASS DOMINANCE TEST
foreach var in tirc_i txi__i clkgxi log_clkgxi ihs_clkgxi pos_tirc_i pos_txi__i pos_clkgxi pos_log_clkgxi pos_ihs_clkgxi  {
gen domtest_`var'=(max1_`var'/sum_`var'>0.8)
gen rvalue_`var'=max1_`var'/sum_`var'
}



* ROUND AMOUNTS TO THE NEAREST 100
foreach var in  tirc_i txi__i clkgxi pos_tirc_i pos_txi__i pos_clkgxi {
	gen r`var'=.
	replace r`var'=round(`var',10) if `var'<=1000
	replace r`var'=round(`var',100) if `var'>1000

}


save "$dir_results\RESTAT_Figure_5.dta", replace

export excel using "$dir_results\RESTAT_Figure_5.xlsx", firstrow(variables) replace
 
}


***************** FIGURE 6B: INTENSIVE MARGIN RESULTS **************************

if (`figure6') {

cap log close
cap log using "$dir_log\RESTAT_Figure_6b.log", replace

* LOGS REGRESSIONS
clear
use "$dir_data\data_sample_DD_intensive.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

* keep only those with positive contributions
keep if clkgxi > 0

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls
set matsize 10000
local demographics age age2 age3  i.fcmp_i  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
foreach g in `groups'   {
   
global cg_8593=`g'
qui sum log_clkgxi if cg_8593==`g' & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593==`g' & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593==`g' & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593==`g'
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=1
reg log_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==`g' | cg_8593==2), vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"


drop if cg_8593==`g' & _n>=1500
}


keep N N_cg_8593 cg_8593_2 version b* se* ave_mtr_zero ave_mtr ave_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr ave_clkgxi version b* se*
save "$dir_results\RESTAT_Figure_6b.dta", replace

* LOGS graphs:
clear
use "$dir_results\RESTAT_Figure_6b.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long b se , i(version N ave_mtr_zero ave_mtr ave_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_6b.xlsx", firstrow(variables) replace


********************************************************************************
***************** FIGURE 6A: EXTENSIVE MARGIN RESULTS **************************

cap log close
cap log using "$dir_log\RESTAT_Figure_6a.log", replace

* LOGS REGRESSIONS
clear
use "$dir_data\data_sample_DD_unconditional.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls
set matsize 10000
local demographics age age2 age3  i.fcmp_i  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
foreach g in `groups'   {
   
global cg_8593=`g'
qui sum pos_clkgxi if cg_8593==`g' & year==1993
global pos_clkgxi =`r(mean)' 
qui sum mtr if cg_8593==`g' & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593==`g' & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593==`g'
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=1
reg pos_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==`g' | cg_8593==2), vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"


drop if cg_8593==`g' & _n>=1500
}


keep N N_cg_8593 cg_8593_2 version b* se* ave_mtr_zero ave_mtr pos_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr pos_clkgxi version b* se*
save "$dir_results\RESTAT_Figure_6a.dta", replace

* Extensive Margin graphs:
clear
use "$dir_results\RESTAT_Figure_6a.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long b se , i(version N ave_mtr_zero ave_mtr pos_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_6a.xlsx", firstrow(variables) replace

}

***************** FIGURE 7: HETEROGENEITY BY AGE RESULTS ***********************

if (`figure7') {

cap log close
cap log using "$dir_log\RESTAT_Figure_7.log", replace

clear
use "$dir_data/data_sample_DD_unconditional.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls

set matsize 10000
local demographics age age2 age3  i.fcmp_i  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
foreach g in `groups'   {
   
global cg_8593=`g'
qui sum ihs_clkgxi if cg_8593==`g' & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593==`g' & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593==`g' & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593==`g'
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=4
reg ihs_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==`g' | cg_8593==2) & age >= 65, vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"

drop if cg_8593==`g' & _n>=1500

}


keep N N_cg_8593 cg_8593_2  version b* se* ave_mtr_zero ave_mtr ave_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr ave_clkgxi version b* se*
save "$dir_results/RESTAT_Figure_7c.dta", replace
*log close


* IHS Unconditional graphs:
clear
use "$dir_results/RESTAT_Figure_7c.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr ave_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_7c.xlsx", firstrow(variables) replace



*** INTENSIVE MARGIN ***
clear
use "$dir_data/data_sample_DD_intensive.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

* keep only those with positive contributions
keep if clkgxi > 0

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls

set matsize 10000
local demographics age age2 age3  i.fcmp_i  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
foreach g in `groups'   {
   
global cg_8593=`g'
qui sum log_clkgxi if cg_8593==`g' & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593==`g' & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593==`g' & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593==`g'
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
reg log_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==`g' | cg_8593==2) & age >= 65, vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"

drop if cg_8593==`g' & _n>=1500
}


keep N N_cg_8593 cg_8593_2  version b* se* ave_mtr_zero ave_mtr ave_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr ave_clkgxi version b* se*
save "$dir_results/RESTAT_Figure_7b.dta", replace


* LOGS Intensive graphs:
clear
use "$dir_results/RESTAT_Figure_7b.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr ave_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_7b.xlsx", firstrow(variables) replace


*** EXTENSIVE MARIGN ***
set more off
set matsize 11000

clear
use "$dir_data/data_sample_DD_unconditional.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls

set matsize 10000
local demographis age age2 age3  i.fcmp_i  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
foreach g in `groups'   {
   
global cg_8593=`g'
qui sum pos_clkgxi if cg_8593==`g' & year==1993
global pos_clkgxi =`r(mean)' 
qui sum mtr if cg_8593==`g' & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593==`g' & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593==`g'
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year


di  " ALTOGETHER"
global version=4
reg pos_clkgxi  ${main_independent} `demographics' `other'  if (cg_8593==`g' | cg_8593==2) & age >= 65, vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"


drop if cg_8593==`g' & _n>=1500
}


keep N N_cg_8593 cg_8593_2 version b* se* ave_mtr_zero ave_mtr pos_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr pos_clkgxi version b* se*
save "$dir_results/RESTAT_Figure_7a.dta", replace


* EXTENSIVE MARGIN GRAPHS: AGE HETEROGENEITY
clear
use "$dir_results\RESTAT_Figure_7a.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr pos_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_7a.xlsx", firstrow(variables) replace

}

***************** FIGURE 8: Heterogeneity by Marial Status *********************

if (`figure8') {

cap log close
cap log using "$dir_log\RESTAT_Figure_8.log", replace

clear
use "$dir_data\data_sample_DD_unconditional.dta"
gen married = (fcmp_i == 1 | fcmp_i == 11 | fcmp_i == 2 | fcmp_i == 12 | fcmp_i == 5 | fcmp_i == 15)

drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls
set matsize 10000
local demographics age age2 age3  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
  
global cg_8593="3-5"
qui sum ihs_clkgxi if cg_8593>=3 & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593>=3 & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593>=3 & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593>= 3 & cg_8593 <= 5
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=1
reg ihs_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5),  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=2
reg ihs_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 1,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=3
reg ihs_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 0,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"

drop if cg_8593>=3 & _n>=1500


keep N N_cg_8593 cg_8593_2  version b* se* ave_mtr_zero ave_mtr ave_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr ave_clkgxi version b* se*
save "$dir_results\RESTAT_Figure_8c.dta", replace


* Unconditional graphs
clear
use "$dir_results\RESTAT_Figure_8c.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr ave_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_8c.xlsx", firstrow(variables) replace


*** INTENSIVE/CONDITIONAL ***
clear
use "$dir_data\data_sample_DD_intensive.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

gen married = (fcmp_i == 1 | fcmp_i == 11 | fcmp_i == 2 | fcmp_i == 12 | fcmp_i == 5 | fcmp_i == 15)

* set as panel data
sort lin__i year
xtset lin__i year

* keep only those with positive contributions
keep if clkgxi > 0

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls
set matsize 10000
local demographics age age2 age3  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1
 
global cg_8593="3-5"
qui sum log_clkgxi if cg_8593>=3 & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593>=3 & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593>=3 & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593>= 3 & cg_8593 <= 5
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=1
reg log_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5),  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=2
reg log_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 1,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=3
reg log_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 0,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"


drop if cg_8593>=3 & _n>=1500



keep N N_cg_8593 cg_8593_2  version b* se* ave_mtr_zero ave_mtr ave_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr ave_clkgxi version b* se*
save "$dir_results\RESTAT_Figure_8b.dta", replace


* Intensive Margin graphs:
clear
use "$dir_results\RESTAT_Figure_8b.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr ave_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_8b.xlsx", firstrow(variables) replace


*** EXTENSIVE MARGIN ***
clear
use "$dir_data\data_sample_DD_unconditional.dta"
drop if year < 1990
drop if year > 1999
drop if flag_losses == 1

gen married = (fcmp_i == 1 | fcmp_i == 11 | fcmp_i == 2 | fcmp_i == 12 | fcmp_i == 5 | fcmp_i == 15)

drop log_clkgxi
gen log_clkgxi = log(clkgxi)

* set as panel data
sort lin__i year
xtset lin__i year

char T_event[omit] -1
xi i.T_event, pref(_)

* set controls
set matsize 10000
local demographics age age2 age3  i.num_sxco_i i.tnkidi  
local other i.province 

gen cg_8593_2=.
gen version=.
gen num_reports_2=.
gen N=.
forvalues y=1(1)10{
gen b`y'=.
gen se`y'=.
}
gen N_cg_8593=.
gen ave_mtr_zero=.
gen ave_mtr=.
gen ave_clkgxi=.

local groups "3 4 5"

global i=1

global cg_8593="3-5"
qui sum pos_clkgxi if cg_8593>=3 & year==1993
global ave_clkgxi =`r(mean)' 
qui sum mtr if cg_8593>=3 & year==1993
global mtr=`r(mean)'
qui sum mtr_zero if cg_8593>=3 & year==1993
global mtr_zero=`r(mean)'
di $mtr_zero
qui sum cg_8593 if cg_8593>= 3 & cg_8593 <= 5
global N_cg_8593=`r(N)'

global main_independent _T_event_1 _T_event_2 _T_event_3 _T_event_5 _T_event_6 _T_event_7 _T_event_8 _T_event_9 _T_event_10 i.cg_8593 i.year

di  " ALTOGETHER"
global version=1
reg pos_clkgxi ${main_independent} `demographics' `other'  if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5),  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=2
reg pos_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 1,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"
global version=3
reg pos_clkgxi ${main_independent} `demographics' `other' if (cg_8593==2 | cg_8593==3 | cg_8593 ==4 | cg_8593 == 5) & married == 0,  vce(cluster lin__i)
do "$dir_do/DiffnDiff_Save.do"


drop if cg_8593>=3 & _n>=1500



keep N N_cg_8593 cg_8593_2 version b* se* ave_mtr_zero ave_mtr pos_clkgxi
order N N_cg_8593 cg_8593_2 ave_mtr_zero ave_mtr pos_clkgxi version b* se*
save "$dir_results\RESTAT_Figure_8a.dta", replace


* EXTENSIVE MARGIN graphs:
clear
use "$dir_results\RESTAT_Figure_8a.dta"
replace b4 = 0
drop if cg_8593_2==.
reshape long   b se , i(version N ave_mtr_zero ave_mtr pos_clkgxi cg_8593_2) j(year)
replace se = 0 if se == .
gen lb=b-1.96*se
gen ub=b+1.96*se
replace year = 1989 + year

gen N_round = round(N,5)
gen N_cg_8593_round = round(N_cg_8593,5)
export excel using "$dir_results\RESTAT_Figure_8a.xlsx", firstrow(variables) replace

}



**************** FIGURE 9: WHO RESPONDED IN 1994? *****************************

if (`figure9') {
	
cap log close
log using "$dir_log\RESTAT_Figure_9.log", replace	

*************** CALCULATE HOW OFTEN REPORTED CAP GAINS IN THE PAST 
 *weighted true
 foreach sample in weighted    {
 
****************************************************************************
clear
use "$dir_data\capital_income_final_8299.dta"
keep lin__i year clkgxi  wgt2_i tirc_i txi__i cpi_to2016
append using  "$dir_data\capital_income_final_0016.dta"
keep lin__i year clkgxi  wgt2_i tirc_i txi__i cpi_to2016
save "$dir_data\capital_income_final_smallcombined.dta", replace

****************************************************************************
use "$dir_data\capital_income_final_smallcombined.dta"
drop tirc_i txi__i 

* merge with age
sort lin__i 
merge lin__i using "$dir_data/demographic_permanent.dta"
keep if _merge!=2
drop _merge
gen age=year-yob__i

*gen age94=age-(year-1994)
gen age_group=1 if age<=40
replace age_group=2 if age>40 & age<=60
replace age_group=3 if age>60
drop  age

sort  lin__i year
xtset lin__i year

* calculate lagged values
forvalues i=1(1)6{
gen clkgxi_m`i'=l`i'.clkgxi
*gen pos_clkgxi_m`i'=(clkgxi_m`i'>0 & clkgxi_m`i'!=.)
}


gen total=1

****** Calculate % with NO reported net capital gains in the past 1,2, 3, 5 and 10 years ********
gen some1=(clkgxi>0)
gen some1_1k=(clkgxi*cpi_to2016>2338.8)

gen some_past3=(clkgxi>0 | (clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.) )
lab var some_past3 "no cap gains this and past 2 years"
gen some_past5=(clkgxi>0 | (clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.) | (clkgxi_m3>0 & clkgxi_m3!=.) | (clkgxi_m4>0 & clkgxi_m4!=.))
lab var some_past5 "no cap gains this and past 4 years"
gen some_past7=(clkgxi>0 | (clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.) | (clkgxi_m3>0 & clkgxi_m3!=.) | (clkgxi_m4>0 & clkgxi_m4!=.) | (clkgxi_m5>0 & clkgxi_m5!=.) | (clkgxi_m6>0 & clkgxi_m6!=.))
lab var some_past7 "no cap gains this and past 6 years"

gen some1_past2=(clkgxi>0 & ((clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.)) )
lab var some1_past2 "no cap gains this and past 2 years"
gen some1_past4=(clkgxi>0 & ((clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.) | (clkgxi_m3>0 & clkgxi_m3!=.) | (clkgxi_m4>0 & clkgxi_m4!=.)))
lab var some1_past4 "no cap gains this and past 4 years"
gen some1_past6=(clkgxi>0 & ((clkgxi_m1>0 & clkgxi_m1!=.) | (clkgxi_m2>0 & clkgxi_m2!=.) | (clkgxi_m3>0 & clkgxi_m3!=.) | (clkgxi_m4>0 & clkgxi_m4!=.) | (clkgxi_m5>0 & clkgxi_m5!=.)  | (clkgxi_m6>0 & clkgxi_m6!=.)))
lab var some1_past6 "no cap gains this and past 6 years"

gen some_past3_1k=(clkgxi*cpi_to2016>2338.8 | (clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.) )
lab var some_past3_1k "no cap gains this and past 2 years"
gen some_past5_1k=(clkgxi*cpi_to2016>2338.8 | (clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.) | (clkgxi_m3*cpi_to2016>2338.8 & clkgxi_m3!=.) | (clkgxi_m4*cpi_to2016>2338.8 & clkgxi_m4!=.))
lab var some_past5_1k "no cap gains this and past 4 years"
gen some_past7_1k=(clkgxi*cpi_to2016>2338.8 | (clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.) | (clkgxi_m3*cpi_to2016>2338.8 & clkgxi_m3!=.) | (clkgxi_m4*cpi_to2016>2338.8 & clkgxi_m4!=.) | (clkgxi_m5*cpi_to2016>2338.8 & clkgxi_m5!=.) | (clkgxi_m6*cpi_to2016>2338.8 & clkgxi_m6!=.))
lab var some_past7_1k "no cap gains this and past 6 years"

gen some1_past2_1k=(clkgxi*cpi_to2016>2338.8 & ((clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.)) )
lab var some1_past2_1k "no cap gains this and past 2 years"
gen some1_past4_1k=(clkgxi*cpi_to2016>2338.8 & ((clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.) | (clkgxi_m3*cpi_to2016>2338.8 & clkgxi_m3!=.) | (clkgxi_m4*cpi_to2016>2338.8 & clkgxi_m4!=.)))
lab var some1_past4_1k "no cap gains this and past 4 years"
gen some1_past6_1k=(clkgxi*cpi_to2016>2338.8 & ((clkgxi_m1*cpi_to2016>2338.8 & clkgxi_m1!=.) | (clkgxi_m2*cpi_to2016>2338.8 & clkgxi_m2!=.) | (clkgxi_m3*cpi_to2016>2338.8 & clkgxi_m3!=.) | (clkgxi_m4*cpi_to2016>2338.8 & clkgxi_m4!=.) | (clkgxi_m5*cpi_to2016>2338.8 & clkgxi_m5!=.)  | (clkgxi_m6*cpi_to2016>2338.8 & clkgxi_m6!=.)))
lab var some1_past6_1k "no cap gains this and past 6 years"

gen clkgxi_some6=clkgxi*cpi_to2016  if  some1_past6==1
gen none1_past6=(clkgxi>0 & some1_past6==0)
gen clkgxi_none6=clkgxi*cpi_to2016  if clkgxi>0 & some1_past6==0

keep year wgt2_i total some* age_group clkgxi* none1_past6

if "`sample'"=="true"{

local by_what year age_group
*  DOMINANCE TEST
foreach var in clkgxi_some6 clkgxi_none6  {
	bysort `by_what': egen max1_`var'=max(`var')
	gen temp=`var' if `var'!=max1_`var'
	bysort year: egen max2_`var'=max(temp)
	replace temp=.
	replace temp=`var' if `var'!=max1_`var' & `var'!=max2_`var'
	bysort `by_what': egen max3_`var'=max(temp)
	gen sum_`var'=`var' if `var'!=max1_`var' & `var'!=max2_`var' & `var'!=max3_`var'
	drop temp max2_`var' max3_`var'
}

collapse  (sum)  total some1 some1_1k some_past3 some_past5 some_past7 some_past3_1k some_past5_1k some_past7_1k none1_past6 ///
		  (sum)    		some1_past2 some1_past4 some1_past6 some1_past2_1k some1_past4_1k some1_past6_1k ///
		  (mean) 	clkgxi_some6 clkgxi_none6 ///
		  (mean) max1_clkgxi_some6 max1_clkgxi_none6  ///
		  (sum)  sum_clkgxi_some6 sum_clkgxi_none6  ///
,   by(year age_group)

foreach var in clkgxi_some6 clkgxi_none6 {
gen domtest_`var'=(max1_`var'/sum_`var'>0.8)
gen rvalue_`var'=max1_`var'/sum_`var'
}

}

if "`sample'"=="weighted"{

collapse  (sum)  total some1 some1_1k some_past3 some_past5 some_past7 some_past3_1k some_past5_1k some_past7_1k none1_past6 ///
		  (sum)    		some1_past2 some1_past4 some1_past6 some1_past2_1k some1_past4_1k some1_past6_1k ///
		  (mean) 	clkgxi_some6 clkgxi_none6	///
 [w=wgt2_i] ,   by(year age_group)
 
 
 * ROUND COUNTS &  inflate by 5 
foreach var in total some1 some1_1k some_past3 some_past5 some_past7 some_past3_1k some_past5_1k some_past7_1k none1_past6 some1_past2 some1_past4 some1_past6 some1_past2_1k some1_past4_1k some1_past6_1k  {
gen r`var'=round(`var'*5,5)
}

* ROUND AMOUNTS 
foreach var in  clkgxi_some6 clkgxi_none6  {
	gen r`var'=.
	replace r`var'=round(`var',10) if `var'<=1000
	replace r`var'=round(`var',100) if `var'>1000
}

}

save "$dir_results\RESTAT_Figure_9_participation_freq_graphs_`sample'.dta", replace
export excel using "$dir_results\RESTAT_Figure_9_participation_freq_graphs_`sample'.xlsx", firstrow(variables) replace	


}
		
}